/*
 * RBFDownload.h
 *
 *  Created on: 2023年7月22日
 *      Author: mcealt_DS
 */

#ifndef RBFDOWNLOAD_H_
#define RBFDOWNLOAD_H_
#include "RBFCommon.h"
#define __packed __attribute__((__packed__))

/*
 * cmd = 0x08  网关主动下发
 * payload: macAdd(1B) + msgSeq(1B) + RSSI(1B) + QrfH1(1B) + QrfH2(nB) + KeyValue(nB)
 *
 */
typedef struct
{
  uint16_t macAdd;  //[16]
  uint8_t msgSeq;   //[18]
  uint8_t msgRetry; //[19]
  uint8_t rssi;     //[20]
  uint8_t qrfH1;    //[21]
  uint8_t qrfH2;    //[22]
  uint8_t keyValueSize;
  uint8_t *keyValue;
} __packed RBFMacMessageGWDownload_t;

typedef enum
{
  RBF_MAC_PAYLOAD_GW_DOWNLOAD_INDEX_FIRST = RBF_MAC_HEADER_INDEX_LAST + 1,
  RBF_MAC_PAYLOAD_GW_DOWNLOAD_INDEX_MACADD = RBF_MAC_PAYLOAD_GW_DOWNLOAD_INDEX_FIRST,
  RBF_MAC_PAYLOAD_GW_DOWNLOAD_INDEX_MSGSEQ = RBF_MAC_PAYLOAD_GW_DOWNLOAD_INDEX_FIRST + 2,
  RBF_MAC_PAYLOAD_GW_DOWNLOAD_INDEX_MSGRETRY = RBF_MAC_PAYLOAD_GW_DOWNLOAD_INDEX_FIRST + 3,
  RBF_MAC_PAYLOAD_GW_DOWNLOAD_INDEX_RSSI = RBF_MAC_PAYLOAD_GW_DOWNLOAD_INDEX_FIRST + 4,
  RBF_MAC_PAYLOAD_GW_DOWNLOAD_INDEX_QRFH1 = RBF_MAC_PAYLOAD_GW_DOWNLOAD_INDEX_FIRST + 5,
  RBF_MAC_PAYLOAD_GW_DOWNLOAD_INDEX_QRFH2 = RBF_MAC_PAYLOAD_GW_DOWNLOAD_INDEX_FIRST + 6,
  RBF_MAC_PAYLOAD_GW_DOWNLOAD_INDEX_KEYVALUE = RBF_MAC_PAYLOAD_GW_DOWNLOAD_INDEX_FIRST + 7,
  RBF_MAC_PAYLOAD_GW_DOWNLOAD_INDEX_LAST = RBF_MAC_PAYLOAD_GW_DOWNLOAD_INDEX_KEYVALUE,
} RBFMAC_PAYLOAD_GW_DOWNLOAD_INDEX_E;

#define RBF_MAC_PAYLOAD_GW_DOWNLOAD_LEN (sizeof(RBFMacMessageGWDownload_t) - sizeof(uint8_t) - sizeof(uint8_t *))

#if defined(PACKET_0_INCLUDE)
#define RBF_MAC_PACKET_GW_DOWNLOAD_LEN (RBF_MAC_PACKET_LEN_SIZE + RBF_HEADER_LENGTH_MAX + RBF_MAC_PAYLOAD_GW_DOWNLOAD_LEN)
#else
#define RBF_MAC_PACKET_GW_DOWNLOAD_LEN (RBF_HEADER_LENGTH_MAX + RBF_MAC_PAYLOAD_GW_DOWNLOAD_LEN)
#endif

/*
 * cmd = 0x09  节点应答
 * payload: BattV(1B) + RSSI(1B) + Temp(1B) + MacAdd(2B) + QrfR1(1B) + QrfR2(1B) + KeyValue(nB)
 *
 */
typedef struct
{
  uint8_t battV;    //[16]
  uint8_t rssi;     //[17]
  uint8_t temp;     //[18]
  uint8_t qrfR1;    //[19]
  uint8_t qrfR2;    //[20]
  uint8_t keyValueSize;
  uint8_t *keyValue;
} __packed RBFMacMessageNodeAck_t;

typedef enum
{
  RBF_MAC_PAYLOAD_NODE_ACK_INDEX_FIRST = RBF_MAC_HEADER_INDEX_LAST + 1,
  RBF_MAC_PAYLOAD_NODE_ACK_INDEX_BATTV = RBF_MAC_PAYLOAD_NODE_ACK_INDEX_FIRST,
  RBF_MAC_PAYLOAD_NODE_ACK_INDEX_RSSI = RBF_MAC_PAYLOAD_NODE_ACK_INDEX_FIRST + 1,
  RBF_MAC_PAYLOAD_NODE_ACK_INDEX_TEMP = RBF_MAC_PAYLOAD_NODE_ACK_INDEX_FIRST + 2,
  RBF_MAC_PAYLOAD_NODE_ACK_INDEX_QRFR1 = RBF_MAC_PAYLOAD_NODE_ACK_INDEX_FIRST + 3,
  RBF_MAC_PAYLOAD_NODE_ACK_INDEX_QRFR2 = RBF_MAC_PAYLOAD_NODE_ACK_INDEX_FIRST + 4,
  RBF_MAC_PAYLOAD_NODE_ACK_INDEX_KEYVALUE = RBF_MAC_PAYLOAD_NODE_ACK_INDEX_FIRST + 5,
  RBF_MAC_PAYLOAD_NODE_ACK_INDEX_LAST = RBF_MAC_PAYLOAD_NODE_ACK_INDEX_KEYVALUE,
} RBFMAC_PAYLOAD_NODE_ACK_INDEX_E;

#define RBF_MAC_PAYLOAD_NODE_ACK_LEN (sizeof(RBFMacMessageNodeAck_t) - sizeof(uint8_t) - sizeof(uint8_t *))

#if defined(PACKET_0_INCLUDE)
#define RBF_MAC_PACKET_NODE_ACK_LEN (RBF_MAC_PACKET_LEN_SIZE + RBF_HEADER_LENGTH_MAX + RBF_MAC_PAYLOAD_NODE_ACK_LEN)
#else
#define RBF_MAC_PACKET_NODE_ACK_LEN (RBF_HEADER_LENGTH_MAX + RBF_MAC_PAYLOAD_NODE_ACK_LEN)
#endif

/*
 * 网关主动下发payload
 */
// 解包
void RBF_gw_download_payload_packetToMsg(uint8_t *packet, RBFMacMessageGWDownload_t *msg);
// 组包
void RBF_gw_download_payload_msgToPacket( RBFMacMessageGWDownload_t msg, uint8_t *packet);


/*
 * 节点应答payload
 */
// 解包
void RBF_node_ack_payload_packetToMsg(uint8_t *packet, RBFMacMessageNodeAck_t *msg);
// 组包
void RBF_node_ack_payload_msgToPacket(RBFMacMessageNodeAck_t msg, uint8_t *packet);





#endif /* RBFDOWNLOAD_H_ */
